package cn.appsys.controller;


import cn.appsys.pojo.BackendUser;
import cn.appsys.service.BackendUserService;
import cn.appsys.tools.Constants;
import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.service.AppCategoryService;
import cn.appsys.service.AppInfoService;
import cn.appsys.service.DataDictionaryService;
import cn.appsys.tools.PageSupport;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.annotation.Resource;
import java.util.List;



@Controller
@RequestMapping("/msg/")
public class ControllerMsg {
 @Resource
    BackendUserService backendUserService;

    @Resource
    private AppInfoService appInfoService;

    @Resource
    private AppCategoryService appCategoryService;
   @RequestMapping("doLogin")
    public String doLogin(){
       return "login/houtailogin";
   }

    @RequestMapping("yanzhengLogin")
    public String yanzhengLogin(@RequestParam("devCode")String username,
                                @RequestParam("devPassword")String password,
                                HttpSession session, Model model){
        BackendUser backendUser =new BackendUser();
        backendUser = backendUserService.loginFlag(username,password);

        if(backendUser== null || backendUser.getUserPassword().equals("") || !backendUser.getUserPassword().equals(password)){
            model.addAttribute("error","用户名或密码错误！");
            return "login/houtailogin";
        }else {

            session.setAttribute("backendUser",backendUser);
            return "Hellohoutai";

        }

    }

    @RequestMapping("loginOut")
    public String loginOut(HttpSession session){
        session.removeAttribute("backendUser");

        return "index";
    }



    @Resource
    private DataDictionaryService dataDictionaryService;

    //显示list页面信息方法
    @RequestMapping("listMsg")
    public String msgList(PageSupport ps, Model model,
                          @RequestParam(value = "swName",required = false)String swName,
                          @RequestParam(value = "status",required = false)String status,
                          @RequestParam(value = "flatformId",required = false)String flatformId,
                          @RequestParam(value = "pid1",required = false)String pid1,
                          @RequestParam(value = "pid2",required = false)String pid2,
                          @RequestParam(value = "pid3",required = false)String pid3){
        if(null == status || "" == status){
            status = "0";
        }
        if(null == flatformId || "" == flatformId){
            flatformId = "0";
        }
        if(null == pid1 || "" == pid1){
            pid1 = "0";
        }else{
            //二级分类
            List<AppCategory> appCategoryList2 = appCategoryService.getAppCategoryById(Integer.valueOf(pid1));
            model.addAttribute("appCategoryList2",appCategoryList2);
        }
        if(null == pid2 || "" == pid2){
            pid2 = "0";
        }else{
            //三级分类
            List<AppCategory> appCategoryList3 = appCategoryService.getAppCategoryById(Integer.valueOf(pid2));
            model.addAttribute("appCategoryList3",appCategoryList3);
        }
        if(null == pid3 || "" == pid3){
            pid3 = "0";
        }

        //根据条件查询出所有软件信息（附带分页）
        List<AppInfo> appInfoList = appInfoService.getAllAppInfoByMsg(ps,swName,Integer.valueOf(status),Integer.valueOf(flatformId),Integer.valueOf(pid1),Integer.valueOf(pid2),Integer.valueOf(pid3));
        model.addAttribute("appInfoList",appInfoList);

        //app状态
        List<DataDictionary> dataDictionaryList = dataDictionaryService.qryAllByCode("APP_STATUS");
        model.addAttribute("dataDictionaryList",dataDictionaryList);

        //所属平台
        List<DataDictionary> dataDictionaries = dataDictionaryService.qryAllByCode("APP_FLATFORM");
        model.addAttribute("dataDictionaries",dataDictionaries);

        //一级分类
        List<AppCategory> appCategoryList1 = appCategoryService.getAppCategoryById(Integer.valueOf("-1"));
        model.addAttribute("appCategoryList1",appCategoryList1);

        //将原来的条件带回页面
        model.addAttribute("swName",swName);
        model.addAttribute("status",status);
        model.addAttribute("flatformId",flatformId);
        model.addAttribute("pid1",pid1);
        model.addAttribute("pid2",pid2);
        model.addAttribute("pid3",pid3);

        //分页返回
        model.addAttribute("ps",ps);

        return "appinfolist";
    }

    //根据一级分类查询二级分类或者根据二级分类查询三级分类
    @RequestMapping("queryCategoryLevel")
    @ResponseBody
    public String getCategoryLevel(@RequestParam(value = "pid1",required = false)String pid1,
                                  @RequestParam(value = "pid2",required = false)String pid2){
        List<AppCategory> appCategoryList1 = null;
        if(null == pid1){
            pid1 = "0";
        }else{
            appCategoryList1 = appCategoryService.getAppCategoryById(Integer.valueOf(pid1));
        }
        if(null == pid2){
            pid2 = "0";
        }else{
            appCategoryList1 = appCategoryService.getAppCategoryById(Integer.valueOf(pid2));
        }
//        List<AppCategory> appCategoryList1 = appCategoryService.getAppCategoryById(Integer.valueOf(pid1));
//        List<AppCategory> appCategoryList2 = appCategoryService.getAppCategoryById(Integer.valueOf(pid2));
        StringBuffer sb = new StringBuffer("{");
        sb.append("\"appCategoryList1\":"+ JSON.toJSON(appCategoryList1));
        sb.append("}");

        return sb.toString();
    }
}
